﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Microsoft.Win32;

using WpfApp4.Models.Caches;

namespace WpfApp4.Views
{
    /// <summary>
    /// InputWindow.xaml 的交互逻辑
    /// </summary>
    public partial class InputWindow : Window
    {
        public InputWindow()
        {
            InitializeComponent();
            inputTxt.Text = CommonCache.Instance.Get("input")?.ToString() ?? string.Empty;
        }

        public string InputData = string.Empty;

        private void EnterKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Confirm();
            }
            else if (e.Key == Key.Escape)
            {
                Cancel();
            }
        }


        private void ConfirmBtnClick(object sender, RoutedEventArgs e)
        {
            Confirm();
        }

        private void CancelBtnClick(object sender, RoutedEventArgs e)
        {
            Cancel();
        }

        private void Confirm()
        {
            CommonCache.Instance.Set("input", inputTxt.Text);
            InputData = inputTxt.Text;
            this.DialogResult = true;
            this.Close();
        }

        private void Cancel()
        {
            this.DialogResult = false;
            this.Close();
        }

        private void inputTxt_PreviewDrop(object sender, DragEventArgs e)
        {
            ((TextBox)sender).Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
        }

        private void inputTxt_PreviewDragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effects = DragDropEffects.Link;
                e.Handled = true;
            }
            else
                e.Effects = DragDropEffects.None;
        }

        private void SelectFileBtnClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "(*.*)|*.*";
            var res = ofd.ShowDialog();
            if (res.HasValue && res.Value)
            {
                inputTxt.Text = ofd.FileName;
            }
        }
    }
}
